﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Extensions.Logging;

namespace Utils.JobTask
{
    public class JobRun
    {
        public static Task Run(Action test, string errMsg = "任务失败")
        {
            return Task.Run(() =>
            {
                try
                {
                    test();
                }
                catch
                {
                    Console.WriteLine(errMsg);
                }
            });
        }

        public static Task Run( Action test, ILogger logger, string errMsg = "任务失败")
        {
            return Task.Run(() =>
            {
                try
                {
                    test();
                }
                catch (Exception e)
                {
                    logger.LogError(e.InnerException ?? e, errMsg);
                }
            });
        }


        public static async Task Run( Func<Task> test, ILogger logger, string errMsg)
        {
            try
            {
                await test();
            }
            catch (Exception e)
            {
                logger.LogError(e.InnerException ?? e, errMsg);
            }
        }

        public static async Task Run( Func<Task> test, string errMsg)
        {
            try
            {
                await test();
            }
            catch 
            {
                Console.WriteLine(errMsg);
            }
        }
    }
}
